﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace WinFormMDI
{
    class COMAid
    {
        private static System.IO.Ports.SerialPort com;
        public static void COMRegister(ComboBox cmbExamples)
        {
            com = new System.IO.Ports.SerialPort();
            //获取所有的串口   
           
            Microsoft.VisualBasic.Devices.Computer pc = new Microsoft.VisualBasic.Devices.Computer();
            cmbExamples.Items.Clear();
            foreach (string s in pc.Ports.SerialPortNames)
            {
                cmbExamples.Items.Add(s);
            }   
        }

        public static void COMRegister(DevExpress.XtraEditors.ComboBoxEdit cmbExamples)
        {
            //com = new System.IO.Ports.SerialPort();
            //string[] ay = System.IO.Ports.SerialPort.GetPortNames();

            //获取所有的串口   
            Microsoft.VisualBasic.Devices.Computer pc = new Microsoft.VisualBasic.Devices.Computer();
            cmbExamples.Properties.Items.Clear();

            ArrayList comList = new ArrayList();
            foreach (string s in pc.Ports.SerialPortNames)
            {
                cmbExamples.Properties.Items.Add(s);
            }
        }

        public static ArrayList GetCOMRegisterList()
        {
            //com = new System.IO.Ports.SerialPort();
            //string[] ay = System.IO.Ports.SerialPort.GetPortNames();

            //获取所有的串口   
            Microsoft.VisualBasic.Devices.Computer pc = new Microsoft.VisualBasic.Devices.Computer();

            ArrayList comList = new ArrayList();
            foreach (string s in pc.Ports.SerialPortNames)
            {
                string strCom = s;
                if (s.Length == 4 && s.IndexOf("COM") >= 0)
                {
                    strCom = s.Replace("COM", "COM0");
                }
                comList.Add(strCom);
            }

            comList.Sort();

            ArrayList retComList = new ArrayList();
            foreach (string s in comList)
            {
                string strcom = s.Replace("COM0", "COM");
                if (!retComList.Contains(strcom))
                {
                    retComList.Add(strcom);
                }
            }

            return retComList;
        }

    }
}
